package com.luyuan.crm.promotion.web;

import javax.annotation.Resource;
import javax.faces.model.ListDataModel;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.luyuan.crm.member.model.MemberLevel;
import com.luyuan.crm.promotion.MemberIntegralPromotion;
import com.luyuan.crm.promotion.MemberIntegralPromotionService;
import com.ruijie.components.setting.Setting;
import com.ruijie.components.setting.service.SettingService;
import com.ruijie.util.StringUtils;
import com.ruijie.web.jsf.action.BaseAction;

@Component("memberIntegralPromotionAction")
@Scope("view")
public class MemberIntegralPromotionAction extends BaseAction {
    @Resource(name = "memberIntegralPromotionService")
    private MemberIntegralPromotionService memberIntegralPromotionService;

    @Resource(name = "settingService")
    private SettingService settingService;

    private MemberIntegralPromotion memberIntegralPromotion;

    private Integer bestowIntegral = null;

    public Integer getBestowIntegral() {
        if (bestowIntegral == null) {
            Setting setting = settingService.getSetting("crm.promotion.registMember.bestowIntegral");
            bestowIntegral = setting.getInteger();
        }
        return bestowIntegral;
    }

    public void setBestowIntegral(Integer bestowIntegral) {
        this.bestowIntegral = bestowIntegral;
    }

    public void updateBestowIntegral() {
        Setting setting = settingService.getSetting("crm.promotion.registMember.bestowIntegral");
        setting.setValue(bestowIntegral);
        settingService.updateSetting(setting);
    }

    public MemberIntegralPromotion getMemberIntegralPromotion() {
        String memberLevel = getRequestParameter("memberLevel");
        if (StringUtils.isNotEmpty(memberLevel)) {
            memberIntegralPromotion = memberIntegralPromotionService
                    .findByMemberLevel(MemberLevel.valueOf(memberLevel));
        }
        return memberIntegralPromotion;
    }

    public void update() {
        memberIntegralPromotionService.updateMemberLevelPromotion(memberIntegralPromotion);

        addGlobalI18nMessage("crud_update_success_msg");
    }

    public ListDataModel getTableModel() {
        return new ListDataModel(memberIntegralPromotionService.getAllMemberLevelPromotion());
    }
}
